'use strict';
const dayjs = require("dayjs");

module.exports = app => {
  const { STRING, INTEGER, DATE, TEXT } = app.Sequelize;
  
  const Users = app.model.define(
    'Users',
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      username: { type: STRING(50), allowNull: false, unique: true },
      password: { type: STRING(255), allowNull: false },
      real_name: { type: STRING(50) },
      phone: { type: STRING(20) },
      email: { type: STRING(100) },
      avatar: { type: STRING(255) },
      role_id: { type: INTEGER, defaultValue: null },
      status: { type: INTEGER, defaultValue: 1 },
      last_login_time: { type: DATE },
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
      update_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'users',
      timestamps: true,
      createdAt: 'create_time',
      updatedAt: 'update_time'
    }
  );

  Users.associate = function() {
    app.model.Users.belongsTo(app.model.Roles, { foreignKey: 'role_id', targetKey: 'id', as: 'role' });
  };

  return Users;
};
